package celiasoft.itasalib.query.result;

import java.util.ArrayList;
import java.util.List;

import celiasoft.itasalib.query.ResultsMetaInfo;
import celiasoft.itasalib.structures.DataElement;

public class QueryResult {

	private QueryResultStatusType _status = QueryResultStatusType.ERROR;
	private QueryResultError _error = null;
	private List<DataElement> _data = null;
	

	private ResultsMetaInfo _meta = null;
	
	public QueryResult(List<DataElement> data, ResultsMetaInfo meta) {
		this(QueryResultStatusType.SUCCES, null, data, meta);
	}

	public QueryResult(QueryResultError error) {
		this(QueryResultStatusType.ERROR, error, null, null);
	}
	
	public QueryResult(QueryResultStatusType status, QueryResultError error,
			List<DataElement> data, ResultsMetaInfo meta) {
		super();
		this._status = status;
		this._error = error;
		this._data = data;
		this._meta = (meta != null)?meta:new ResultsMetaInfo();
	}
	
	public void setStatusType(QueryResultStatusType status){
		this._status = status;
	}
	
	public QueryResultStatusType getStatusType(){
		return this._status;
	}
	
	public boolean isStatusOk(){
		return (getStatusType() == QueryResultStatusType.SUCCES);
	}
	
	
	public void setQueryResultError(QueryResultError error){
		this._error = error;
	}
	
	public QueryResultError getQueryResultError(){
		if(!this.isStatusOk())
			return this._error;
		
		return null;
	}
	
	
	public void setDataResult(List<DataElement> data){
		this._data = data;
	}
	
	public List<DataElement> getDataResult(){
		if(this.isStatusOk())
			return this._data;
		
		return null;
	}

	public void addSingleDataResult(DataElement data){
		if(this._data == null)
			this._data = new ArrayList<DataElement>();
		
		this._data.add(data);
	}

	/**
	 * @return the moreData
	 */
	public boolean isThereMoreData() {
		
		return this._meta.isThereMoreData();
	}
	
	public void thereIsMoreData(boolean moreData) {
		this._meta.thereIsMoreData(moreData);
	}
}
